/**
 * Copyright 2009-2015. qiuxj. All rights reserved. <a>http://www.firebird.com</a>
 */
package com.firebird.commons.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author qiuxj create on 2009-3-22
 *
 */
public class DateUtil {
	
	/**
	 * format a date Object to a appoint style (eg:"yyyy-MM-dd HH:mm:ss" or "yyyy-MM-dd")
	 * @param date
	 * @param formatStyle
	 * @return
	 */
	public static String format(Date date,String formatStyle) {
		String result = "";
		if(date == null || formatStyle == null){
			result = "";
		}
		try {
			SimpleDateFormat style = new SimpleDateFormat(formatStyle);
			result = style.format(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	/**
	 * format a date Object to "yyyy-MM-dd HH:mm:ss"
	 * @param date
	 * @return
	 */
	public static String format(Date date){
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * format a date Object to "yyyy-MM-dd"
	 * @param date
	 * @return
	 */
	public static String formatToDay(Date date){
		return format(date, "yyyy-MM-dd");
	}
	
	/**
	 * get now date and format to "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static String now() {
		Calendar cal = Calendar.getInstance();
		Date now = cal.getTime();
		return format(now);
	}
	
	/**
	 * get now date and sum some day format to "yyyy-MM-dd HH:mm:ss"
	 * @param dayNum
	 * @return
	 */
	public static String sumNDay(int dayNum) {
		Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.set(Calendar.DAY_OF_YEAR, day + dayNum);
        Date date = calendar.getTime();
		return format(date);
	}
	
	/**
	 * get now date and sum some month format to "yyyy-MM-dd HH:mm:ss"
	 * @param monthNum
	 * @return
	 */
	public static String sumNMonth(int monthNum) {
		Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month + monthNum);
        Date date = calendar.getTime();
		return format(date);
	}
	
	/**
	 * get now date and sum some year format to "yyyy-MM-dd HH:mm:ss"
	 * @param yearNum
	 * @return
	 */
	public static String sumNYear(int yearNum) {
		Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, year + yearNum);
        Date date = calendar.getTime();
		return format(date);
	}
	
	/**
	 * get now date and sum some hour format to "yyyy-MM-dd HH:mm:ss"
	 * @param hourNum
	 * @return
	 */
	public static String sumNHour(int hourNum) {
		Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.set(Calendar.HOUR_OF_DAY, hour + hourNum);
        Date date = calendar.getTime();
		return format(date);
	}
	
	/**
	 * get now date and sum some minute format to "yyyy-MM-dd HH:mm:ss"
	 * @param minuteNum
	 * @return
	 */
	public static String sumNMinute(int minuteNum) {
		Calendar calendar = Calendar.getInstance();
        int minute = calendar.get(Calendar.MINUTE);
        calendar.set(Calendar.MINUTE, minute + minuteNum);
        Date date = calendar.getTime();
		return format(date);
	}
	
	/**
	 * get now date and sum some second format to "yyyy-MM-dd HH:mm:ss"
	 * @param secondNum
	 * @return
	 */
	public static String sumNSecond(int secondNum) {
		Calendar calendar = Calendar.getInstance();
        int second = calendar.get(Calendar.SECOND);
        calendar.set(Calendar.SECOND, second + secondNum);
        Date date = calendar.getTime();
		return format(date);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.err.println(now()+"\n"+sumNSecond(10));
	}

}
